梦入琼楼寒有月,行过石树冻无烟

PHP 文件访问

在PHP语言之中,常用的有四种文件访问方法,分别为**”include“、”require“、”include_once“、”require_once”**四种,使用方法如下:

include

indexef.php

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>demo</title>
</head>
<body>
<?php
$one = "Hello,";
$two = "World!";
?>
</body>
</html>

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>demo</title>
</head>
<body>
<?php
include 'indexef.php';
echo "$one $two";
?>
</body>
</html>

文件错误提示

require

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>demo</title>
</head>
<body>
<?php
require 'indexef.php';
echo "$one $two";
echo "<br>";
?>
</body>
</html>

错误提示

include_once!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>demo</title>
</head>
<body>
<?php
include_once 'indexef.php';
echo "$one $two";
echo "<br>";

?>
</body>
</html>

文件错误提示

require_once

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>demo</title>
</head>
<body>
<?php
require_once 'indexef.php';
echo "$one $two";
?>
</body>
</html>
ID DA FA
include 用于访问文件[^one.php可以使用two.php的相关函数]
require 用于访问文件[^one.php可以使用two.php的相关函数]
include_once 用于访问文件[^one.php可以使用two.php的相关函数]
require_once 用于访问文件[^one.php可以使用two.php的相关函数]
⬅️ Go back